<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2021/7/5
 * Time: 5:27 下午
 */

use app\lib\exception\ParamsException;
use app\middleware\CheckAuthorization;
use think\facade\Route;
// 获取多版本header 默认1.0.0
$version = request()->header('x-api-version', '1.0.0');
$perfix = '';
if ($version != '1.0.0') {
    $versionArr = explode('.', $version);
    if (empty($versionArr) || count($versionArr) != 3) {
        throw new ParamsException('版本号不正确');
    }
    list($first, $mid, $end) = $versionArr;
    $perfix = 'v' . str_pad($first, 2, '0', STR_PAD_LEFT) . str_pad($mid, 2, '0', STR_PAD_LEFT) . str_pad($end, 2, '0', STR_PAD_LEFT) . '.';
}
Route::group('users', function () {
    // 验证码
    Route::get('captcha', 'captcha');
    // 登录
    Route::post('login', 'login');
    // 刷新token
    Route::post('refresh-token', 'refreshToken');
    // 用户信息
    Route::get('info', 'userInfo')->middleware(CheckAuthorization::class, 'get@users/info');
    // 用户详情
    Route::get('/:id', 'read')->middleware(CheckAuthorization::class, 'get@users/:id');
    // 编辑用户
    Route::put('/:id', 'update')->middleware(CheckAuthorization::class, 'put@users/:id');
    // 删除用户
    Route::delete('/:id', 'delete')->middleware(CheckAuthorization::class, 'delete@users/:id');
    // 新增用户
    Route::post('', 'save')->middleware(CheckAuthorization::class, 'post@users');
    // 用户列表
    Route::get('', 'userList')->middleware(CheckAuthorization::class, 'get@users');
})->prefix($perfix . 'Users/');

Route::group('menus', function () {
    // 更新菜单
    Route::put('/:id', 'update')->middleware(CheckAuthorization::class, 'put@menus/:id');
    // 删除菜单
    Route::delete('/:id', 'delete')->middleware(CheckAuthorization::class, 'delete@menus/:id');
    // 新增菜单
    Route::post('', 'save')->middleware(CheckAuthorization::class, 'post@menus');
    // 菜单树
    Route::get('tree', 'tree')->middleware(CheckAuthorization::class, 'get@menus/tree');
    // 菜单详情
    Route::get('/:id', 'read')->middleware(CheckAuthorization::class, 'get@menus/:id');
})->prefix('Menus/');

Route::group('roles', function () {
    // 角色下成员
    Route::get('/:id/members', 'memberList')->middleware(CheckAuthorization::class, 'get@roles/:id/members');
    // 角色添加成员
    Route::post('/:id/members', 'addMember')->middleware(CheckAuthorization::class, 'post@roles/:id/members');
    // 角色移除成员
    Route::delete('/:id/members', 'removeMember')->middleware(CheckAuthorization::class, 'delete@roles/:id/members');
    // 角色菜单授权
    Route::post('/:id/menus', 'bindMenu')->middleware(CheckAuthorization::class, 'post@roles/:id/menus');
    // 角色绑定的菜单
    Route::get('/:id/menus', 'menuList')->middleware(CheckAuthorization::class, 'get@roles/:id/menus');
    // 删除角色
    Route::delete('/:id', 'delete')->middleware(CheckAuthorization::class, 'delete@roles/:id');
    // 更新角色
    Route::put('/:id', 'update')->middleware(CheckAuthorization::class, 'put@roles/:id');
    // 读取角色
    Route::get('/:id', 'read')->middleware(CheckAuthorization::class, 'get@roles/:id');
    // 新增角色
    Route::post('', 'save')->middleware(CheckAuthorization::class, 'post@roles');
    // 角色列表
    Route::get('', 'roleList')->middleware(CheckAuthorization::class, 'get@roles');
})->prefix('Roles/');

// 文件上传
Route::group('file', function () {
    // 分片上传
    Route::group('chunk', function () {
        // 检测文件是否存在
        Route::get('presence', 'chunkPresence')->middleware(CheckAuthorization::class, 'get@file/chunk/presence');
        // 合并切片
        Route::post('merge', 'chunkMerge')->middleware(CheckAuthorization::class, 'post@file/chunk/merge');
        // 上传切片
        Route::post('', 'chunk')->middleware(CheckAuthorization::class, 'post@file/chunk');
    });
    // 普通文件上传
    Route::post('', 'upload')->middleware(CheckAuthorization::class, 'pist@file');
})->prefix('File/');


// 验证码
Route::get('captcha', 'Users/captcha');

Route::get('test-redis', 'Users/testRedis');
